package org.dromara.kitchen.controller;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.mybatis.core.page.PageUtils;
import org.dromara.kitchen.domain.kitchen.KitchenChef;
import org.dromara.kitchen.service.IKitchenChefService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 厨师
 *
 * @author Mr.Zhou
 * @date 2024-12-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/kitchen/chef")
public class KitchenChefController extends BaseController {

    private final IKitchenChefService kitchenChefService;

    /**
     * 查询厨师列表
     */
    @SaCheckPermission("kitchen:chef:list")
    @GetMapping("/list")
    public TableDataInfo<KitchenChef> list(KitchenChef kitchenChef, PageQuery pageQuery) {
        QueryWrapper<KitchenChef> qw = new QueryWrapper<>();

        TableDataInfo<KitchenChef> td = PageUtils.buildDataInfo(kitchenChefService.page(PageUtils.buildPage(),qw));
        List<KitchenChef> list = td.getRows();
        return td;
    }

    /**
     * 导出厨师列表
     */
    @SaCheckPermission("kitchen:chef:export")
    @Log(title = "厨师", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(KitchenChef kitchenChef, HttpServletResponse response) {
        List<KitchenChef> list = kitchenChefService.list();
        ExcelUtil.exportExcel(list, "厨师", KitchenChef.class, response);
    }

    /**
     * 获取厨师详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("kitchen:chef:query")
    @GetMapping("/{id}")
    public R<KitchenChef> getInfo(@NotNull(message = "主键不能为空") @PathVariable Integer id) {
        KitchenChef chef = kitchenChefService.getById(id);
        if(chef!= null){
            String[] areas = { chef.getProvId() + "",chef.getCityId() + "", chef.getDistrictId() + "" };
            chef.setProvinceArray(areas);
        }
        return R.ok(chef);
    }

    /**
     * 新增厨师
     */
    @SaCheckPermission("kitchen:chef:add")
    @Log(title = "厨师", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody KitchenChef kitchenChef) {
        if(kitchenChef.getProvinceArray()!=null && kitchenChef.getProvinceArray().length >= 2){
            kitchenChef.setProvId(Integer.parseInt(kitchenChef.getProvinceArray()[0]));
            kitchenChef.setCityId(Integer.parseInt(kitchenChef.getProvinceArray()[1]));
            kitchenChef.setDistrictId(Integer.parseInt(kitchenChef.getProvinceArray()[2]));
        }
        return toAjax(kitchenChefService.save(kitchenChef));
    }

    /**
     * 修改厨师
     */
    @SaCheckPermission("kitchen:chef:edit")
    @Log(title = "厨师", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody KitchenChef kitchenChef) {
        if(kitchenChef.getProvinceArray()!=null && kitchenChef.getProvinceArray().length >= 2){
            kitchenChef.setProvId(Integer.parseInt(kitchenChef.getProvinceArray()[0]));
            kitchenChef.setCityId(Integer.parseInt(kitchenChef.getProvinceArray()[1]));
            kitchenChef.setDistrictId(Integer.parseInt(kitchenChef.getProvinceArray()[2]));
        }
        return toAjax(kitchenChefService.updateById(kitchenChef));
    }

    /**
     * 删除厨师
     *
     * @param ids 主键串
     */
    @SaCheckPermission("kitchen:chef:remove")
    @Log(title = "厨师", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Integer[] ids) {
        return toAjax(kitchenChefService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
